function toolbarMove(ele, e) {
    var osmall = ele.parentNode;
    osmall.style.cursor = "move";
    var e = e || window.event;
    /*用于保存小的div拖拽前的坐标*/
    osmall.startX = e.clientX - osmall.offsetLeft;
    osmall.startY = e.clientY - osmall.offsetTop;
    /*鼠标的移动事件*/
    document.onmousemove = function(e) {
        var e = e || window.event;
        osmall.style.left = e.clientX - osmall.startX + "px";
        osmall.style.top = e.clientY - osmall.startY + "px";
        /*对于大的DIV四个边界的判断*/
        if (e.clientX - osmall.startX <= 0) {
            osmall.style.left = 0 + "px";
        }
        if (e.clientY - osmall.startY <= 0) {
            osmall.style.top = 0 + "px";
        }
        //if (e.clientX - osmall.startX >= 1100) {
        //    osmall.style.left = 1100 + "px";
        //}
        if (e.clientY - osmall.startY >= 600) {
            osmall.style.top = 600 + "px";
        }
    };
    /*鼠标的抬起事件,终止拖动*/
    document.onmouseup = function() {
        document.onmousemove = null;
        document.onmouseup = null;
        osmall.style.cursor = "pointer";
    };
}

//全局清除接口
function RemoveAllLayers() {
    removeAllEntities();
    //Analysis.VideoFlag = false;
    //clearAllMapGraphic(); //三维联测
    Intervisibility_clear(true);
    // if (Intervisibility_polygonHandler) {
    //     Intervisibility_clear(true); //通视分析
    // }
    flood_clear(true); //淹没
    clearViewerdPoint(true) //可视域分析
        //if (skyline_polygonHandler) { //天际线
        //    Skyline_clear();
        //}
    handler_pick ? handler_pick.destroy() : null;
    profileClear(true);
    slopeGradientClear(true) //坡度坡向
    //Terrainexcavation_clear(true);
    if (FillandCutPolygon) { //填挖方
        FillandCut_clear(true);
    }
    if (dynamicMonomerHandler) {
        dynamicMonomerHandler.destroy();
        dynamicMonomerHandler = null;
    }
    Crop_clear(true);
    layer.closeAll(true);
    identifyClear(true);
    ClearSnowOrRain(true);
    clearRollerShutter(true);
    //分屏
    //if (splitType != "layout_style_1") {
    //    splitType = "layout_style_1";
    //    layout(1, 1);
    //}
    terrainClipPlan && terrainClipPlan.clear();
}

/**
 * 返回uuid
 */
function getUUID() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";

    var uuid = s.join("");
    return uuid;
}

/**
 * 清空所以实体
 * @param {*} type 
 */
function removeAllEntities(type) {
    for (var i = 0; i < viewer.entities.values.length;) {
        var entityId = viewer.entities.values[i].id;
        if (entityId && (entityId.indexOf("video_entity_") > -1 || entityId.indexOf("story_") > -1)) {
            i++;
        } else {
            viewer.entities.remove(viewer.entities.values[i]);
        }
    }
    //for (var i = 0; i < viewer.scene.primitives._primitives.length;) {
    //    var primitive = viewer.scene.primitives._primitives[i];
    //    if (primitive.url) {
    //        i++;
    //    } else {
    //        viewer.scene.primitives.remove(viewer.scene.primitives._primitives[i]);
    //    }
    //}
    if (featuresDataSource) {
        featuresDataSource.entities.removeAll();
    }
}

function getPickPositionGlobe(position) {
    var cartesian = null;
    if (viewer.scene.globe.depthTestAgainstTerrain) {
        //cartesian = viewer.camera.pickEllipsoid(position, viewer.scene.globe.ellipsoid);
        cartesian = viewer.scene.pickPosition(position);
    } else {
        var ray = viewer.camera.getPickRay(position);
        cartesian = viewer.scene.globe.pick(ray, viewer.scene);
    }
    return cartesian;
}

/**
 * 弹出信息框
 * @param {*} text 
 * @param {*} timeout 
 * @param {*} btnText 
 */
function showMsgLayer(text, timeout, btnText) {
    layui.layer.msg(text, {
        icon: 0,
        time: timeout ? timeout : 2000,
    });
    //layui.layer.msg('<div style="padding: 20px 100px;">' + text + '</div>', {
    //    time: timeout ? timeout : 2000,
    //    btn: [(btnText ? btnText : '确定')]
    //});
}

//定位到图层位置
function flyTo3DViewByLayerInfo(id, loadLayerInfo, positions) {
    //获取要定位的图层信息
    var locationLayer = loadLayerInfo[id] ? loadLayerInfo[id] : loadLayerInfo;
    if (locationLayer["SERVICETYPE"] === sw_servicetype.sw_oblique || locationLayer["SERVICETYPE"] === sw_servicetype.sw_gltf) {
        return viewer.zoomTo(cesium.layer3DList[id]);
    } else if (locationLayer["SERVICETYPE"] === sw_servicetype.sw_terrain) {
        return null;
    }
    var positions = locationLayer.POSITION;
    if (positions != "") {
        positions = positions.split(',');
    }

    if (positions.length < 3) { //没有缩放级别时，补空
        positions.push("");
    }

    //图层上移
    //var layerHelp = new LayerHelper(locationLayer);
    //var thisLayer = layerHelp.GetLayer();
    //layerHelp.moveToUp();

    if (positions != "") {
        if (positions[2]) {
            MapStatic.flyTo({ lon: parseFloat(positions[0]), lat: parseFloat(positions[1]), height: (parseFloat(positions[2]) + 1000) });
        } else {
            MapStatic.flyTo({ lon: parseFloat(positions[0]), lat: parseFloat(positions[1]), height: Math.ceil(viewer.camera.positionCartographic.height) });
        }
    }
}

//获取视图范围
function GetViewExtent() {
    var extent = {};
    var scene = viewer.scene;
    var ellipsoid = scene.globe.ellipsoid;
    var canvas = scene.canvas;

    var car3_lt = viewer.camera.pickEllipsoid(new Cesium.Cartesian2(0, 0), ellipsoid); // canvas左上角
    var car3_rb = viewer.camera.pickEllipsoid(new Cesium.Cartesian2(canvas.width, canvas.height), ellipsoid); // canvas右下角

    // 当canvas左上角和右下角全部在椭球体上
    if (car3_lt && car3_rb) {
        var carto_lt = ellipsoid.cartesianToCartographic(car3_lt);
        var carto_rb = ellipsoid.cartesianToCartographic(car3_rb);
        extent.xmin = Cesium.Math.toDegrees(carto_lt.longitude);
        extent.ymax = Cesium.Math.toDegrees(carto_lt.latitude);
        extent.xmax = Cesium.Math.toDegrees(carto_rb.longitude);
        extent.ymin = Cesium.Math.toDegrees(carto_rb.latitude);
    } else if (!car3_lt && car3_rb) { // 当canvas左上角不在但右下角在椭球体上
        var car3_lt2 = null;
        var yIndex = 0;
        var xIndex = 0;
        do {
            // 这里每次10像素递加，一是10像素相差不大，二是为了提高程序运行效率
            yIndex <= canvas.height ? yIndex += 10 : canvas.height;
            xIndex <= canvas.width ? xIndex += 10 : canvas.width;
            car3_lt2 = viewer.camera.pickEllipsoid(new Cesium.Cartesian2(xIndex, yIndex), ellipsoid);
        } while (!car3_lt2);
        var carto_lt2 = ellipsoid.cartesianToCartographic(car3_lt2);
        var carto_rb2 = ellipsoid.cartesianToCartographic(car3_rb);
        extent.xmin = Cesium.Math.toDegrees(carto_lt2.longitude);
        extent.ymax = Cesium.Math.toDegrees(carto_lt2.latitude);
        extent.xmax = Cesium.Math.toDegrees(carto_rb2.longitude);
        extent.ymin = Cesium.Math.toDegrees(carto_rb2.latitude);
    } else if (car3_lt && !car3_rb) { // 当canvas左上角在但右下角不在椭球体上
        var car3_rb2 = null;
        var yIndex = canvas.height;
        var xIndex = canvas.width;
        do {
            // 这里每次10像素递加，一是10像素相差不大，二是为了提高程序运行效率
            yIndex >= 10 ? yIndex -= 10 : 10;
            xIndex >= 10 ? xIndex -= 10 : 10;
            car3_rb2 = viewer.camera.pickEllipsoid(new Cesium.Cartesian2(yIndex, yIndex), ellipsoid);
        } while (!car3_rb2);
        var carto_lt2 = ellipsoid.cartesianToCartographic(car3_lt);
        var carto_rb2 = ellipsoid.cartesianToCartographic(car3_rb2);
        extent.xmin = Cesium.Math.toDegrees(carto_lt2.longitude);
        extent.ymax = Cesium.Math.toDegrees(carto_lt2.latitude);
        extent.xmax = Cesium.Math.toDegrees(carto_rb2.longitude);
        extent.ymin = Cesium.Math.toDegrees(carto_rb2.latitude);
    } else if (!car3_lt && !car3_rb) {
        var car3_lt2 = null;
        var yIndex = 0;
        var xIndex = 0;
        do {
            // 这里每次10像素递加，一是10像素相差不大，二是为了提高程序运行效率
            yIndex <= canvas.height ? yIndex += 10 : canvas.height;
            xIndex <= canvas.width ? xIndex += 10 : canvas.width;
            car3_lt2 = viewer.camera.pickEllipsoid(new Cesium.Cartesian2(xIndex, yIndex), ellipsoid);
        } while (!car3_lt2);

        var car3_rb2 = null;
        var yIndex = canvas.height;
        var xIndex = canvas.width;
        do {
            // 这里每次10像素递加，一是10像素相差不大，二是为了提高程序运行效率
            yIndex >= 10 ? yIndex -= 10 : 10;
            xIndex >= 10 ? xIndex -= 10 : 10;
            car3_rb2 = viewer.camera.pickEllipsoid(new Cesium.Cartesian2(yIndex, yIndex), ellipsoid);
        } while (!car3_rb2);

        var carto_lt2 = ellipsoid.cartesianToCartographic(car3_lt2);
        var carto_rb2 = ellipsoid.cartesianToCartographic(car3_rb2);
        extent.xmin = Cesium.Math.toDegrees(carto_lt2.longitude);
        extent.ymax = Cesium.Math.toDegrees(carto_lt2.latitude);
        extent.xmax = Cesium.Math.toDegrees(carto_rb2.longitude);
        extent.ymin = Cesium.Math.toDegrees(carto_rb2.latitude);
    }

    // 获取高度
    //extent.height = Math.ceil(viewer.camera.positionCartographic.height);
    return [extent.xmin, extent.ymin, extent.xmax, extent.ymax];
}

function handleToGeojson(data, type) {
    var geojson = {
        "type": "FeatureCollection",
        "features": []
    };
    type = type ? type : "arcgis";
    switch (type) {
        case "arcgis":
            for (var i = 0; i < data.length; i++) {
                var param = data[i];
                var arcgis = {
                    displayFieldName: "",
                    fieldAliases: {},
                    fields: [],
                    features: [param],
                    geometryType: param.geometryType,
                    spatialReference: param.geometry.spatialReference
                }
                if (param.geometryType == "esriGeometryPolygon" || param.geometry.rings) {
                    geojson.features.push({
                        "type": "Feature",
                        "geometry": {
                            "type": "Polygon",
                            "coordinates": [param.geometry.rings]
                        },
                        "properties": param.attributes,
                        "arcgis": arcgis
                    });
                } else if (param.geometryType == "esriGeometryPolyline" || param.geometry.path || param.geometry.paths) {
                    geojson.features.push({
                        "type": "Feature",
                        "geometry": {
                            "type": "Polyline",
                            "coordinates": [param.geometry.path || param.geometry.paths]
                        },
                        "properties": param.attributes,
                        "arcgis": arcgis
                    });
                } else if (param.geometryType == "esriGeometryPoint" || param.geometry.x) {
                    geojson.features.push({
                        "type": "Feature",
                        "geometry": {
                            "type": "Point",
                            "coordinates": [param.geometry.x, param.geometry.y]
                        },
                        "properties": param.attributes,
                        "arcgis": arcgis
                    });
                }
            }
            break;
        default:
            break;
    }
    return geojson;
    //示例数据格式
    //var data = {
    //    "type": "FeatureCollection",
    //    "features": [
    //      { "type": "Feature",
    //          "geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
    //          "properties": {"prop0": "value0"}
    //      },
    //      { "type": "Feature",
    //          "geometry": {
    //              "type": "LineString",
    //              "coordinates": [
    //                [102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
    //              ]
    //          },
    //          "properties": {
    //              "prop0": "value0",
    //              "prop1": 0.0
    //          }
    //      },
    //      { "type": "Feature",
    //          "geometry": {
    //              "type": "Polygon",
    //              "coordinates": [
    //                [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],
    //                  [100.0, 1.0], [100.0, 0.0] ]
    //              ]
    //          },
    //          "properties": {
    //              "prop0": "value0",
    //              "prop1": {"this": "that"}
    //          }
    //      }
    //    ]
    //}
}

function getCameraViewCenter() {
    var rectangle = viewer.camera.computeViewRectangle();
    var west = rectangle.west / Math.PI * 180;
    var north = rectangle.north / Math.PI * 180;
    var east = rectangle.east / Math.PI * 180;
    var south = rectangle.south / Math.PI * 180;
    var centerx = (west + east) / 2;
    var centery = (north + south) / 2;
    return [centerx, centery];
}